program files
! This program Transfers Files from the Fluke 9100 to the Sun System
! RS232 port.  A 'C' language program on the Sun System receives these requests
! and transfers the files.  The flow is shown as follows:
!
!MAIN MENU PROGRAM FLOW
!       Fluke                           Sun System
!       -------                 -----   ----------------------
! 1.    Enter Directory & File Name             .        --                -----
! 2.    Send Backspace ($8) for Upload          .       ---->   Enter Upload() Routine.
! 3.                            <---    Send "DIR" Command.
! 4.    if "DIR", send Directory name                   ---->   Received "/XXXX/XXXXX".
! 5.                            <---    Send "NAME" request.
! 6.    if "NAME",send File Name or '*'                 ---->   if filename, call upload_file()
!                                       if '*', call upload_*()
!
!UPLOAD() PROGRAM FLOW
!       Fluke                           Sun System
!       -------                 -----   ----------------------
!
!UPLOAD_*() PROGRAM FLOW
!       Fluke                           Sun System
!       -------                 -----   ----------------------
! 6.    Sent '*'                        ---->   called upload_*()
! 7.                                    executes
!                                       (system "ls * > temp_file").
! 8.                            <----   Send Backspace character ($8).
! 9.    Send 'SEND' command                     ---->
!10.    Open Filename                   <----   if 'SEND', send first filename.
!11.    After opening file, return 'send'                       ---->
!12.    save text file.                 <----   if 'send' received, start
!                                       sending the file.
!13.                            <----   Send Form Feed to indicate
!                                       End Of File.
!14.                            <----   Send Backspace ($8).
!15.    Repeat 8 - 14 until Filename = HALT.




declare
  !global numeric t1i
  !global numeric t1o
  !global numeric winf
  !global numeric winsub
  !global numeric win1
  !global numeric win2
  !global numeric win3
  !global numeric win4
  !global numeric led_win
  !global numeric crt_win

  global numeric dir_win                   ! Display File Selection Menu
  global numeric error_win                 ! Display Error Messages
  global numeric keybdi                    ! Keyboard Input
  global numeric keybdo                    ! Keyboard Output
  global numeric port_out                  ! RS232 Port 2 Output
  global numeric port_in                   ! Rs232 Port 2 Input

  global numeric track
  global numeric finished

! title_msg appears on top line of menu
  global string title_msg = "FILE TRANSFER PROGRAM (Sun to Fluke)"
  global string error_msg = "ERROR"
  global string directory                   ! Sun Directory Location
  global string filename                    ! Filename in Sun Directory
  global string temp_string                 ! Temporary String Storage
  global string message                     ! Error Message to Error Window

  global string clr_scrn = "\1B[2J"
  global string cur_on = "\1B[?25h"
  global string cur_off = "\1B[?25l"
  global string rev = "\1B[7m"
  global string norm = "\1B[0m"
  global string bold = "\1B[1m"
  global string lf_on = "\1B[20h"
  global string lf_off = "\1B[20l"
  global string cur_left = "\1B[1D"         ! move one char to left

  global string array[1:3] fmsg
  global string array[1:21] smsg
  global string array[1:17] tmsg
  ! persistent numeric cntrl_all = 0
end declare

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function purge
  declare global numeric keybdi
  char = ""
  loop while poll (keybdi,"input")
    input on keybdi, char
  end loop
end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function purge_port
  declare global numeric port_in
  char = ""
  loop while poll (port_in,"input")
    input on port_in, char
  end loop
end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function pause
  declare global numeric keybdi
  declare string ch
  ch = ""
  loop until poll (keybdi,"input")
  end loop
  input on keybdi,ch
return(ch)
end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function error(message)
  declare
     string message
     global numeric error_win
     global string clr_scrn
  end declare

print using clr_scrn, on error_win
print on error_win, message

winctl channel error_win, position "unhide"

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
!@ This function inputs the name while allowing backspacing and    @
!@ deletion of the characters that are being input by the operator.@
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

function enter(name)
  declare
     string name
     global numeric keybdi
     global numeric dir_win

  end declare

  char = ""
  name = ""
  chr_count = 14                            ! print characters in column 14
  purge()                           ! flush keyboard

  loop until (char = chr($D))       ! input characters until 'return' key

  input on keybdi, char             ! input from programmers keyboard

  if (char = chr($7F)) then         ! if delete key
      if (chr_count > 14) then
         chr_count = chr_count -1
      end if
      print using "\1B[4;"+str(chr_count,10)+"H ",on dir_win ! print space
      length = len(name)
      if (length > 0) then
         temp = mid(name,1,length-1)
         name = temp
      end if
     else if (char = chr($08)) then  ! if backspace key
       if (chr_count > 14) then
          chr_count = chr_count -1
       end if
       print using "\1B[4;"+str(chr_count,10)+"H ",on dir_win !print space
       length = len(name)
       if (length > 0) then
          temp = mid(name,1,length-1)
          name = temp
       end if
     else if ( char <> chr($D) ) then ! if return key
       name = name + char
       print using "\1B[4;"+str(chr_count,10)+"H#",on dir_win,char
       chr_count = chr_count + 1
  end if


  end loop                    !  END LOOP until (char = chr($D))

return(name)

end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 function output_port(output)
  declare
   string output
   global numeric port_out
  end declare
  !string length, then output for string length
  print on port_out, output

 end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 function input_port
  declare
    global numeric dir_win
    global numeric port_in
  end declare
  ! receive message from sun
input_data = ""
count = 0
print using "\1B[3;2H?#",on dir_win,"BUSY:"
loop while (poll channel port_in, event "input") = 0
  print using "\1B[3;10H?#",on dir_win,"****"
  wait (50)
  print using "\1B[3;10H?#",on dir_win,"    "
  count = count +1
  if (count > 2000) then
      input_data = "ERROR - No Data Received, RS232 Port Timed Out"
      return (input_data)
  end if
end loop

 input on port_in, input_data
 !print on dir_win,"Received : ", input_data
 return (input_data)
 end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 function input_file(filename)
  declare
   string filename
   string file
   string info
   numeric done
   numeric file_in
   numeric temp
   global numeric dir_win
   global numeric port_in
 end declare

 done = 0
 file = ""
 info = ""

 loop while ( poll(port_in,"input") ) = 0           ! waiting for input
     wait (10)
 end loop

 loop until ( instr(file,chr($C)) )                 ! loop until formfeed
     input on port_in, file
     if ( instr(file,"ERROR") )then                 ! if ERROR, return
        error(file)
        info = ""
        return(info)
     end if
     if ( instr(file,"HALT") ) then                 ! if HALT, return
        print using "\1B[6;2H?#",on dir_win,"Sun Halted File download!"
        info = "HALT"
        return(info)
     end if
 end loop

 temp = len filename
 if (temp > 10) then
     error("NAME TOO LONG:" + filename)
     loop while done = 0
       loop while ( poll(port_in,"input") ) = 1  ! waiting for input
          print using "\1B[3;10H?#",on dir_win,"****"
          input on port_in, file                 ! input a line
          print using "\1B[3;10H?#",on dir_win,"    "
          if ( instr(file,chr($C)) ) then           ! if FF,get file name and open
             info = ""
             return(info)                               ! last file sent, return
          end if
       end loop                                ! END LOOP while poll port_in

     end loop                             ! END LOOP while done = 0
 end if

 file_in = open device filename, as "output"        ! Open the file to save
 print using "\1B[4;2H?#",on dir_win,"                                      "
 print using "\1B[4;2HSaving: ?#",on dir_win,filename ! Display filename
 loop while done = 0
  loop while ( poll(port_in,"input") ) = 1  ! waiting for input
     input on port_in, file                 ! input a line
     if ( instr(file,chr($C)) ) then                ! if FF,get file name and open
         close(file_in)
         info = ""
         return(info)                               ! last file sent, return
     end if

    print on file_in, file                 ! save a line to disk
  end loop                                ! END LOOP while poll port_in
 end loop                             ! END LOOP while done = 0

 close(file_in)
 return(info)

 end function
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
!@                  MAIN PROGRAM                                  @
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

char = ""

open()
keybdi = open device "/term2", as "input", mode "unbuffered"
!keybdi = open device "/term2", as "input", mode "buffered"
keybdo = open device "/term2", as "output",mode "unbuffered"
led_win = open device "/term1/win",as "output",mode "unbuffered"
!crt_win = open device "/term2/win",as "output",mode "unbuffered"
port_out = open device "/port2", as "output", mode "buffered"
port_in = open device "/port2", as "input", mode "buffered"

dir_win = open device "/term2/win",as "output",xorg 0,yorg 0,xdim 80,ydim 18,border title_msg
error_win = open device "/term2/win",as "output",xorg 3,yorg 19,xdim 76,ydim 5,border error_msg

winctl channel dir_win, position "hide"
winctl channel error_win, position "hide"
print on dir_win, cur_off+lf_on+norm
print on error_win, cur_off+lf_on+norm

!
  smsg[1]  = "Enter Sun Directory. "
  smsg[2]  = "(eg., /work/cosmos/func/fluke/current_rev/11470_CPU/program"
  smsg[3]  = "Directory: "

  smsg[4]  = "Enter filename to be transferred."
  smsg[5]  = "(eg., AMAIN or * for all files.)"
  smsg[6]  = "Filename: "

winctl channel dir_win, position "unhide"
  ! ------------------   upload files loop   -----------------------!
loop until(char = chr($81) )                     ! upload files until
winctl channel error_win, position "hide"        ! the F1 key is pressed
print using clr_scrn, on error_win

print using clr_scrn, on dir_win

print using "\1B[2;2H?#",on dir_win,smsg[1]
print using "\1B[3;2H?#",on dir_win,smsg[2]
print using "\1B[4;2H?#",on dir_win,smsg[3]

directory = enter (directory)

print using clr_scrn, on dir_win
print using "\1B[2;2H?#",on dir_win,smsg[4]
print using "\1B[3;2H?#",on dir_win,smsg[5]
print using "\1B[4;2H?#",on dir_win,smsg[6]

filename = enter (filename)

purge_port()                               ! clear RS232 port
print using clr_scrn, on dir_win
print using "\1B[2;2H?#",on dir_win,"FILE: " + directory + "/" + filename
temp_string = directory
directory = ""

! **  Send control character(backspace) to Sun to indicate File UPLOAD **
directory = chr($8)
!print using "\1B[3;2H?#",on dir_win,"*"
output_port(directory)
received = ""
received = input_port()
if ( instr(received,"DIR") = 0 ) then
   error("no dir received - " + received)
else if ( instr(received,"ERROR") ) then
   error(received)
end if

! ** Send Directory Name to Sun (Received NAME from Sun)       **********
!print using "\1B[3;3H?#",on dir_win,"*"
directory = temp_string
output_port(directory)
received = ""
received = input_port()
if ( instr(received,"NAME") ) = 0 then
   error("No NAME received - " + received)
else if ( instr(received,"ERROR") ) then
   error(received)
end if

! ******* Send File Name to Sun (Receive SEND from Sun)  ****************
!print using "\1B[3;4H?#",on dir_win,"*"
output_port(filename)
received = ""
received = input_port()
if ( instr(received,"ACK")) = 0 then
   error("No File Name received at the SUN!- " + received)
else if ( instr(received,"ERROR") ) then
   error(received)
end if

! ******** Send 'send' to the Sun  (Received the File back)   ************

return_string = ""

if ( instr(filename,"*")) then                ! Multiple file upload

  loop until ( instr(return_string,"HALT") )   ! Save files until HALT
    output_port("send_name")
    return_string = ""
    return_string = input_port()
    if ( instr(return_string,"ERROR") ) then
       error(return_string)
    else
       if ( instr(return_string,"HALT") ) then    ! the next version !!!!
           !return to the menu
       else
           return_string = input_file(return_string)
       end if              ! END IF "HALT"
    end if               ! END IF "ERROR"
  end loop

else                                        ! Single File upload
   ! input text file from Sun
   return_string = input_file(filename)

end if

print using "\1B[8;2H?#",on dir_win,"DOWNLOAD COMPLETE - Press any key to continue!"


char = pause()                     ! wait for keyboard input

end loop                           ! loop until char = chr($81)
                                   ! upload directories until
                                   ! the F1 key is pressed
close (keybdi)
close (keybdo)
close (port_out)
close (port_in)
close (dir_win)
close (error_win)

!close (t1o)
!close (winm)
!close (winf)


end program


